var APP = require("../../../lib/main/app");
var LIST = require("../../../lib/data/HList");
var REFRESH = require("../../../lib/other/refresh");
var page = {
	is_pass:'',
	categoryid:'',
	type:'',//1考试 2练习
	ready: function() {
		var that = this;
		try{
			let lang =  api.getGlobalData({key: 'lang'})  || 'cn';
			i18n("[i18n]", {
				lang: lang,// 变更语言
				filePath: "i18n/",
				get: true
			});
		}catch(e){
			//TODO handle the exception
		}
		// 头部处理
		var headerHeight = $('#header').height();
		$('body').css('padding-top', headerHeight);
		// 下拉刷新
		REFRESH.refresh('.aui-refresh-content', function(res) {
			if(res.status == "success"){
				LIST.resetData();
			}
			setTimeout(function() {
				REFRESH.cancelLoading(); //隐藏下拉刷新
			}, 4000)
		})
		that.initData();
	},
	on: function() {
		var that = this;
		//批量事件start
		var events = [
			//打开页面
			['#add-channel', function() {

			}],
			// 关闭页面
			['#close', function() {
				APP.w.closeWin();
			}],
			// 搜索
			['#searchBtn', function() {
				that.getExamLog();
			}],
			// 重置
			['#reseat', function() {
				that.is_pass = '';
				that.categoryid = '';
				that.type = '';
				$('.date-box input').val('');
				$('.form-lis .select').removeClass('active');
				that.getExamLog();
			}],
			// 选择状态
			['.status-box .select', function() {
				$(this).addClass('active').siblings().removeClass('active');
				that.is_pass = $(this).data('type');
			}],
			// 选择分类
			['.type-box .select', function() {
				$(this).addClass('active').siblings().removeClass('active');
				that.type = $(this).data('type');
			}],
			// 选择科目
			['.object-box .select', function() {
				$(this).addClass('active').siblings().removeClass('active');
				that.categoryid = $(this).data('id');
			}]
		];
		APP.e.more(events);
		//批量事件end
		
		// 时间选择
		$('#start_date').on('focus', function() {
			$(this).trigger('blur');
		})
		$('#end_date').on('focus', function() {
			$(this).trigger('blur');
		})
		selectMonth('start_date');
		selectMonth('end_date');
		function selectMonth(startId){
			var myDate = new Date();
			var _year = myDate.getFullYear();//获取年
			var _name = 'calendar'+startId;
			_name = new datePicker();
			_name.init({
				'trigger': '#'+startId, /*按钮选择器，用于触发弹出插件*/
				'type': 'date',/*模式：date日期；datetime日期时间；time时间；ym年月；*/
				// 'minDate':(new Date().getFullYear()-2)+'-01',/*最小日期*/
				// 'maxDate':new Date().getFullYear()+'-'+(new Date().getMonth()+1),/*最大日期*/
				'onSubmit':function(){/*确认时触发事件*/
					$('#'+startId).val(_name.value);
				},
				'onClose':function(){/*取消时触发事件*/
				}
			});
		};
	},
	methods: {
		//加载数据
		initData: function() {
			var that = this;
			that.getCategoryList();
			that.getExamLog();
		},
		//查询数据
		getExamLog() {
			var that = this;
			var start_date = $('#start_date').val();
			var end_date = $('#end_date').val();
			var _date1 = new Date(start_date).getTime(); 
			var _date2 = new Date(end_date).getTime(); 
			let toast1 = i18n.get("select.start.time") || '请选择开始时间！';
			let toast2 = i18n.get("select.end.time") || '请选择结束时间！';
			let toast3 = i18n.get("select.start.end.time") || '结束时间不能小于开始时间！';
			if(start_date){
				if(!end_date){
					API.errorToast(toast2);
					return false;
				}
			}
			if(end_date){
				if(!start_date){
					API.errorToast(toast1);
					return false;
				}
			}
			if(_date2 - _date1 < 0 && start_date && end_date){
				API.errorToast(toast3);
				return false;
			}
			let params = {
				is_pass:that.is_pass,
				category:that.categoryid,
				type:that.type,
				start_time:start_date,
				end_time:end_date,
			}
			var api_url = 'exam/get_exam_log';
			var render = require('./list');
			var renderBox = '#list-box';
			var emptyHtml = '';
			//list展示方式
			LIST.init({
				api: api_url,
				params: params,
				page:1,
				pageSize:15,//这个是服务器例如默认10个告诉你的，不传则默认是10个，给前端判断用的，只是为了测试请求6个
				// headRefresh:1,
				dataChildKey:'data',
				// debug:'empty',//调试数据为空显示
				// formatKey: 'works',//处理对象
				emptyHtml:emptyHtml,
				render: render,
				renderBox : renderBox,
				isCallback: 'all',
				success: function(res) {
					REFRESH.cancelLoading(); //隐藏下拉刷新
					// console.log('success:' + JSON.stringify(res) );
					// if(res.data.data.length < 10 && res.data.current_page > 1) {
					// 	$(".no-data").removeClass('hidden');
					// }else{
					// 	$(".no-data").addClass('hidden');
					// }
				}
			});
		},
		//获取分类
		getCategoryList() {
			var that = this;
			let _txt = i18n.get("select.account")
			APP.r.get({
				api: 'category/get_list',
				params: {},
				success: function(res) {
					var _data = res.data;
					$('#object-box').html(`<span i18="select.account">${_txt}</span>`);
					var _html = "";
					try{
						that.categoryId = _data[0].id;
						for (var i = 0; i < _data.length; i++) {
							_html += `<i class="select" data-id = "${_data[i].id}" >${_data[i].category}</i>`;
						}
					}catch(e){
						//TODO handle the exception
					}
					$('#object-box').append(_html);
				}
			});
		},
	}
}
API.run(page);
